package com.chonor.mall.service.impl;

import cn.hutool.core.date.DateUtil;
import com.chonor.mall.dto.MemberSearchDTO;
import com.chonor.mall.enums.GenderEnum;
import com.chonor.mall.enums.UserStatusEnum;
import com.chonor.mall.mapper.MemberMapper;
import com.chonor.mall.model.Member;
import com.chonor.mall.model.MemberExample;
import com.chonor.mall.model.MemberLevel;
import com.chonor.mall.service.MemberLevelService;
import com.chonor.mall.service.MemberService;
import com.chonor.mall.util.EnumUtil;
import com.chonor.mall.vo.MemberDetailVO;
import com.chonor.mall.vo.MemberListVO;
import com.chonor.mall.vo.MemberVO;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private MemberLevelService memberLevelService;


    @Override
    public List<MemberVO> getMemberList(String keyword, Integer pageSize, Integer pageNum) {
        Long count = count();
        PageHelper.startPage(pageNum, pageSize);
        List<Member> members = memberMapper.selectByExample(new MemberExample());
        if(members.size() > 0){
            List<MemberVO> memberVOS = new ArrayList<>(members.size());
            for (Member m : members) {
                MemberVO vo = convert(m);
                memberVOS.add(vo);
            }
            return memberVOS;
        }
        return null;
    }

    @Override
    public MemberVO getItem(Long id) {
        Member member = memberMapper.selectByPrimaryKey(id);
        if(Objects.isNull(member)) return null;
        return convert(member);
    }

    @Override
    public int updateStatus(Long id, Integer status) {
        Member member = memberMapper.selectByPrimaryKey(id);
        if(Objects.isNull(member)) return 0;
        member.setStatus(status);
        int count = memberMapper.updateByPrimaryKeySelective(member);
        return count;
    }

    /**
     * 计算记录数
     * @return
     */
    private Long count(){
        return memberMapper.countByExample(new MemberExample());
    }

    /**
     * 转换member对象为对应的VO对象
     * @param source
     * @return
     */
    private MemberVO convert(Member source){
        MemberVO target = new MemberVO();
        BeanUtils.copyProperties(source,target);
        target.setGender(EnumUtil.getMessage(GenderEnum.class, source.getGender()));
        target.setBirthday(DateUtil.formatDate(source.getBirthday()));
        target.setStatus(EnumUtil.getMessage(UserStatusEnum.class, source.getStatus()));
        target.setCreateat(DateUtil.formatDate(source.getCreateat()));
        if(!Objects.isNull(source.getMemberLevelId())){
            MemberLevel item = memberLevelService.getItem(source.getMemberLevelId());
            target.setMemberLevel(item.getName());
        }
        return target;
    }





































}
